package com.gscitysfy.cus.drain.modules.command.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.gscitysfy.cus.drain.config.BusinessException;
import com.gscitysfy.cus.drain.modules.command.convert.ReservePlanManageConvert;
import com.gscitysfy.cus.drain.modules.command.dto.ReservePlanManageDto;
import com.gscitysfy.cus.drain.modules.command.entity.ReservePlanManage;
import com.gscitysfy.cus.drain.modules.command.mapper.ReservePlanManageMapper;
import com.gscitysfy.cus.drain.modules.command.service.IReservePlanManageService;
import com.gscitysfy.cus.drain.modules.command.vo.ReservePlanManageQuery;
import lombok.AllArgsConstructor;
import org.apache.commons.lang3.StringUtils;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.ArrayList;
import java.util.List;
import java.util.Objects;

/**
 * <p>
 * 服务实现类
 * </p>
 *
 * @author Administrator
 * @since 2022-10-22 11:21:35
 */
@AllArgsConstructor
@Service
public class ReservePlanManageServiceImpl extends ServiceImpl<ReservePlanManageMapper, ReservePlanManage> implements IReservePlanManageService {


    private final ReservePlanManageConvert reservePlanManageConvert;

    @Override
    public IPage<ReservePlanManageDto> fetchPage(Page page, ReservePlanManageQuery query) {
        LambdaQueryWrapper<ReservePlanManage> queryWrapper = Wrappers.lambdaQuery();
        queryWrapper = buildQueryWrapper(queryWrapper, query);
        Page<ReservePlanManage> reservePlanManagePage = this.page(page, queryWrapper);
        return reservePlanManagePage.convert(reservePlanManageConvert::toDto);
    }

    @Override
    public ReservePlanManageDto fetchById(Long cmKid) {
        ReservePlanManage reservePlanManage = baseMapper.selectById(cmKid);
        return reservePlanManageConvert.toDto(reservePlanManage);
    }

    @Transactional(rollbackFor = Throwable.class)
    @Override
    public Boolean saveOrUpdate(ReservePlanManageDto dto) {
        if (Objects.isNull(dto.getCmKid())) {
            ReservePlanManage reservePlanManage = new ReservePlanManage();
            reservePlanManageConvert.toEntity(reservePlanManage, dto);
            return this.save(reservePlanManage);
        } else {
            ReservePlanManage reservePlanManage = baseMapper.selectById(dto.getCmKid());
            reservePlanManageConvert.toEntity(reservePlanManage, dto);
            return this.updateById(reservePlanManage);
        }
    }

    @Transactional(rollbackFor = Throwable.class)
    @Override
    public Boolean delete(List<Long> idList) {
        List<ReservePlanManage> reservePlanManageList = new ArrayList<>();
        idList.forEach(id -> {
            ReservePlanManage reservePlanManage = baseMapper.selectById(id);
            //启用中的禁止删除
            if (reservePlanManage.getEnableStatus()) {
                throw new BusinessException(String.format("该预案：%s已启动，禁止删除", reservePlanManage.getName()));
            }
            reservePlanManage.setCmUsable(true);
            reservePlanManageList.add(reservePlanManage);
        });
        return this.updateBatchById(reservePlanManageList);
    }

    @Transactional(rollbackFor = Throwable.class)
    @Override
    public Boolean enable(Long cmKid) {
        ReservePlanManage reservePlanManage = baseMapper.selectById(cmKid);
        reservePlanManage.setEnableStatus(!reservePlanManage.getEnableStatus());
        return this.updateById(reservePlanManage);
    }

    /**
     * query组装
     *
     * @param queryWrapper
     * @param query
     * @return
     */
    public LambdaQueryWrapper<ReservePlanManage> buildQueryWrapper(LambdaQueryWrapper<ReservePlanManage> queryWrapper, ReservePlanManageQuery query) {
        if (StringUtils.isNotEmpty(query.getName())) {
            queryWrapper.like(ReservePlanManage::getName, query.getName());
        }
        if (StringUtils.isNotEmpty(query.getType())) {
            queryWrapper.eq(ReservePlanManage::getType, query.getType());
        }
        if (Objects.nonNull(query.getEnableStatus())) {
            queryWrapper.eq(ReservePlanManage::getEnableStatus, query.getEnableStatus());
        }
        //删除标识
        queryWrapper.eq(ReservePlanManage::getCmUsable, query.getCmUsable());
        //更新时间倒排
        queryWrapper.orderByDesc(ReservePlanManage::getCmUptime);
        return queryWrapper;
    }
}
